﻿#include "fxpz.h"
#include "ui_fxpz.h"

extern QString yztype;

fxpz::fxpz(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::fxpz)
{
    ui->setupUi(this);
    QButtonGroup * BtnGroupA = new QButtonGroup;
    QButtonGroup * BtnGroupB = new QButtonGroup;

    BtnGroupA->addButton(ui->radioButton,0);
    BtnGroupA->addButton(ui->radioButton_2,1);
    BtnGroupA->setExclusive(true);//使m_BtnA1与m_BtnA2互斥

    BtnGroupB->addButton(ui->radioButton_3,0);
    BtnGroupB->addButton(ui->radioButton_4,1);
    BtnGroupB->setExclusive(true);

    ui->label_9->setVisible(false);
    ui->lineEdit_3->setVisible(false);

    ui->comboBox->blockSignals(true);

    ui->comboBox->addItem("三点均值");
    ui->comboBox->addItem("五点二阶");

    ui->comboBox->blockSignals(false);

    ui->comboBox_2->blockSignals(true);

    ui->comboBox_2->addItem("分析算法1");
    ui->comboBox_2->addItem("分析算法2");

    ui->comboBox_2->blockSignals(false);

    ui->comboBox_3->blockSignals(true);

    ui->comboBox_3->addItem("相对阈值");
    ui->comboBox_3->addItem("绝对阈值");

    ui->comboBox_3->blockSignals(false);

    ui->comboBox_4->blockSignals(true);

    ui->comboBox_4->addItem("FAM");
    ui->comboBox_4->addItem("ROX");
    ui->comboBox_4->addItem("CY5");
    ui->comboBox_4->addItem("VIC");

    ui->comboBox_4->blockSignals(false);

    ui->comboBox_5->blockSignals(true);

    ui->comboBox_5->addItem("阈值算法1");
    ui->comboBox_5->addItem("阈值算法2");

    ui->comboBox_5->blockSignals(false);

}

fxpz::~fxpz()
{
    delete ui;
}

void fxpz::showEvent(QShowEvent *event)
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select * From DataAnalyseSetting";
    query.exec(sql);
    while(query.next()){
        if(query.value("isLv").toString()=="0"){
            ui->radioButton->setChecked(true);
            ui->comboBox->setEnabled(true);

        }else {
            ui->radioButton_2->setChecked(true);
            ui->comboBox->setEnabled(false);

        }
        ui->comboBox->setCurrentText(query.value("LvType").toString());
        ui->comboBox_2->setCurrentText(query.value("AnalyseType").toString());
        if(query.value("AnalyseType").toString()=="分析算法1"){
            ui->comboBox_2->setCurrentText("分析算法2");
        }else{
            ui->comboBox_2->setCurrentText("分析算法1");
        }
        ui->comboBox_2->setCurrentText(query.value("AnalyseType").toString());
        ui->comboBox_3->setCurrentText(query.value("YuType").toString());
        if(query.value("baseType").toString()=="0"){
            ui->radioButton_3->setChecked(true);
        }else {
            ui->radioButton_4->setChecked(true);
        }
        ui->comboBox_3->setCurrentText("FAM");
        ui->lineEdit->setText(query.value("famBeginNum").toString());
        ui->lineEdit_2->setText(query.value("famEnd").toString());
        ui->comboBox_5->setCurrentText(query.value("YuAnalyseType").toString());
        ui->lineEdit_3->setText(query.value("Spe").toString());
    }

    ui->comboBox_4->setCurrentText("FAM");
}

void fxpz::on_radioButton_clicked()
{
    ui->comboBox->setEnabled(true);
}

void fxpz::on_radioButton_2_clicked()
{
    ui->comboBox->setEnabled(false);
}

void fxpz::on_comboBox_2_currentIndexChanged(const QString &arg1)
{
    if(arg1=="分析算法1"){
        ui->radioButton_4->setEnabled(false);
        ui->radioButton_3->setEnabled(true);
        ui->comboBox_3->setEnabled(false);
        ui->comboBox_3->setCurrentText("绝对阈值");
        ui->radioButton_3->click();
    }else{
        ui->radioButton_4->setEnabled(true);
        ui->comboBox_3->setEnabled(true);
        ui->radioButton_4->click();
        ui->lineEdit_3->setEnabled(false);
        ui->radioButton_3->setEnabled(false);
    }
}

void fxpz::on_comboBox_3_currentIndexChanged(const QString &arg1)
{
    yztype = arg1;
    if(arg1=="绝对阈值"){
        ui->radioButton_3->setEnabled(true);
        if(ui->comboBox_2->currentText()=="分析算法2"){
            ui->radioButton_4->setEnabled(true);
        }else{
            ui->radioButton_4->setEnabled(false);
        }
        ui->lineEdit->setEnabled(true);
        ui->lineEdit_2->setEnabled(true);
        ui->comboBox_5->setEnabled(false);
        ui->comboBox_4->setEnabled(true);
    }
    else if(arg1=="相对阈值"){
        ui->radioButton_3->setEnabled(false);
        ui->radioButton_4->setEnabled(false);
        ui->lineEdit->setEnabled(false);
        ui->lineEdit_2->setEnabled(false);
        ui->comboBox_4->setEnabled(false);
        ui->comboBox_5->setEnabled(true);
    }
}

void fxpz::on_radioButton_3_clicked()
{
    ui->comboBox_4->show();
    ui->lineEdit->show();
    ui->lineEdit_2->show();
    ui->lineEdit_3->hide();
    ui->label_5->show();
    ui->label_6->show();
    ui->label_9->hide();
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Update DataAnalyseSetting set baseType=0";
    query.exec(sql);
    ui->comboBox_4->setCurrentText("FAM");
}

void fxpz::on_radioButton_4_clicked()
{
    ui->comboBox_4->hide();
    ui->lineEdit->hide();
    ui->lineEdit_2->hide();
    ui->lineEdit_3->show();
    ui->label_5->hide();
    ui->label_6->hide();
    ui->label_9->show();
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Update DataAnalyseSetting set baseType=1";
    query.exec(sql);
}

void fxpz::on_comboBox_4_currentIndexChanged(const QString &arg1)
{
    if(arg1=="FAM")
    {
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select famBeginNum,famEnd from DataAnalyseSetting";
        query.exec(sql);
        while (query.next())
        {
            ui->lineEdit->setText(query.value("famBeginNum").toString());
            ui->lineEdit_2->setText(query.value("famEnd").toString());
        }
    }
    else if(arg1=="ROX")
    {
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select roxBeginNum,roxEndNum from DataAnalyseSetting";
        query.exec(sql);
        while (query.next())
        {
            ui->lineEdit->setText(query.value("roxBeginNum").toString());
            ui->lineEdit_2->setText(query.value("roxEndNum").toString());
        }
    }
    else if(arg1=="CY5")
    {
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select cy5BeginNum,cy5EndNum from DataAnalyseSetting";
        query.exec(sql);
        while (query.next())
        {
            ui->lineEdit->setText(query.value("cy5BeginNum").toString());
            ui->lineEdit_2->setText(query.value("cy5EndNum").toString());
        }
    }
    else if(arg1=="VIC")
    {
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select vicBeginNum,vicEndNum from DataAnalyseSetting";
        query.exec(sql);
        while (query.next())
        {
            ui->lineEdit->setText(query.value("vicBeginNum").toString());
            ui->lineEdit_2->setText(query.value("vicEndNum").toString());
        }
    }
}

void fxpz::on_lineEdit_editingFinished()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql;
    if(ui->comboBox_4->currentText()=="FAM"){
        sql=QString("Update DataAnalyseSetting set famBeginNum='%1'").arg(ui->lineEdit->text());

    }else if(ui->comboBox_4->currentText()=="ROX"){
        sql=QString("Update DataAnalyseSetting set roxBeginNum='%1'").arg(ui->lineEdit->text());

    }else if(ui->comboBox_4->currentText()=="CY5"){
        sql=QString("Update DataAnalyseSetting set cy5BeginNum='%1'").arg(ui->lineEdit->text());

    }else if(ui->comboBox_4->currentText()=="VIC"){
        sql=QString("Update DataAnalyseSetting set vicBeginNum='%1'").arg(ui->lineEdit->text());

    }
    query.exec(sql);
}

void fxpz::on_lineEdit_2_editingFinished()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql;
    if(ui->comboBox_4->currentText()=="FAM"){
        sql=QString("Update DataAnalyseSetting set famEnd='%1'").arg(ui->lineEdit_2->text());

    }else if(ui->comboBox_4->currentText()=="ROX"){
        sql=QString("Update DataAnalyseSetting set roxEndNum='%1'").arg(ui->lineEdit_2->text());

    }else if(ui->comboBox_4->currentText()=="CY5"){
        sql=QString("Update DataAnalyseSetting set cy5EndNum='%1'").arg(ui->lineEdit_2->text());

    }else if(ui->comboBox_4->currentText()=="VIC"){
        sql=QString("Update DataAnalyseSetting set vicEndNum='%1'").arg(ui->lineEdit_2->text());

    }
    query.exec(sql);
}


void fxpz::on_pushButton_3_clicked()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString Spe=ui->lineEdit_3->text();
    QString YuAnalyseType=ui->comboBox_5->currentText();
    QString YuType=ui->comboBox_3->currentText();
    QString AnalyseType=ui->comboBox_2->currentText();
    QString LvType=ui->comboBox->currentText();
    QString isLv;
    if(ui->radioButton_2->isChecked()){
        isLv="1";
    }else{
        isLv="0";
    }
    QString sql=QString("Update DataAnalyseSetting set Spe='%1',YuAnalyseType='%2',YuType='%3',AnalyseType='%4',LvType='%5',isLv='%6'").
            arg(Spe,YuAnalyseType,YuType,AnalyseType,LvType,isLv);
    query.exec(sql);

    if(ui->comboBox_2->currentText()=="分析算法1")
    {
        dataAnaly = new DataAnalysisThread();
        dataAnaly->run(false);
    }
    else if(ui->comboBox_2->currentText()=="分析算法2")
    {
        dataAnalyJ=new DataAnalysisByJThread();
        dataAnalyJ->run(false);
    }
    this->close();
}

void fxpz::on_pushButton_2_clicked()
{
    this->close();
}

